package com.yundasys.lab02springbootstudy.registrar;

import com.yundasys.lab02springbootstudy.service.HelloService;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AssignableTypeFilter;

import java.util.Map;

/**
 * @author jiangta
 * @version 1.0
 * @date 2021/2/2 1:29 下午
 */
public class HelloImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * @Author haien
     * @Description AnnotationMetadata:当前类的注解信息；
     * BeanDefinitionRegistry：注册类，其registerBeanDefinition()可以注册bean
     * @Date 2019/6/12
     * @Param [importingClassMetadata, registry]
     * @return void
     **/
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        //扫描注解
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
        String[] basePackages = (String[])annotationAttributes.get("basePackages");

        ClassPathBeanDefinitionScanner scanner
                = new ClassPathBeanDefinitionScanner(registry, false);
        AssignableTypeFilter assignableTypeFilter = new AssignableTypeFilter(HelloService.class);
        scanner.addIncludeFilter(assignableTypeFilter);
        scanner.scan(basePackages);

    }
}
